package world.snowcrystal.openapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import world.snowcrystal.commons.exception.BusinessException;
import world.snowcrystal.commons.model.entity.InterfaceInfo;
import world.snowcrystal.commons.model.enums.ApplicationResponseStatusCode;
import world.snowcrystal.commons.service.InterfaceInfoService;
import world.snowcrystal.openapi.exception.ThrowUtils;
import world.snowcrystal.openapi.mapper.InterfaceInfoMapper;
/**
 * 接口信息服务实现类
 *
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService {

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {


        if (interfaceInfo == null) {
            throw new BusinessException(ApplicationResponseStatusCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String requestHeader = interfaceInfo.getRequestHeader();
        String responseHeader = interfaceInfo.getResponseHeader();
        Integer status = interfaceInfo.getStatus();
        String method = interfaceInfo.getMethod();
        Long userid = interfaceInfo.getUserId();

        ThrowUtils.throwIf(userid == null , ApplicationResponseStatusCode.PARAMS_ERROR, "用户id不能为空");

        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(name,method,url)) {
                throw new BusinessException(ApplicationResponseStatusCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ApplicationResponseStatusCode.PARAMS_ERROR, "名称过长");
        }
        if (StringUtils.isNotBlank(description) ) {
            ThrowUtils.throwIf(description.length() > 255, ApplicationResponseStatusCode.PARAMS_ERROR, "描述过长");
        }
    }

}




